package com.zhnc.dangjianpingtaiapp.utils.recycler;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
//            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
//            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
//
//            if (position < spanCount) { // top edge
//                outRect.top = spacing;
//            }
//            outRect.bottom = spacing; // item bottom

            outRect.left = spacing;
            outRect.right = spacing;
            outRect.bottom = spacing;
            outRect.top = spacing;
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }

    //        this.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                TLog.e("newState:" + newState);
//                state = newState;
//
//                if (newState == 0) {
//                    HGridView.this.stopScroll();
//                    int abs = Math.abs(dx2);
//                    if (abs > screenWidth) {
//                        if (dx2 >= 0) {
//                            page++;
//                            if(page>maxPage){
//                                page = maxPage;
//                            }
//                            HGridView.this.smoothScrollToPosition((page+1)*pageNum -1);
//                        } else {
//                            page--;
//                            page--;
//                            if (page<0){
//                                page=0;
//                            }
//                            if(page == 0){
//                                HGridView.this.smoothScrollToPosition(0);
//                            }else{
//                                HGridView.this.smoothScrollToPosition((page+1)*pageNum -1);
//                            }
//                        }
//                    } else {
//                        if(page == 0){
//                            HGridView.this.smoothScrollToPosition(0);
//                        }else{
//                            HGridView.this.smoothScrollToPosition((page+1)*pageNum -1);
//                        }
//                    }
//
//                    dx2 = 0;
//                }
//                if (newState == 1) {
//                    dx2 = 0;
//                }
//            }
//
//            @Override
//            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                TLog.e("dx:" + dx + "  dy:" + dy);
//                if (state == 1) {
//                    dx2 += dx;
//                }
//
//            }
//        });
}
